package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.snmp.SnmpUtils;
import cn.sccl.dirms.monitor.model.Host;
import org.snmp4j.smi.OID;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * snmp接口
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/snmp")
public class SnmpController {

    /**
     * 获取交换机端口和ip的关系
     * @param host 核心交换机IP,为了方便传值，这里只是用host对象的name值来传
     */
    @RequestMapping(value = "/getExchangeIPAndPort", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String, Object> getExchangeIPAndPort(@RequestBody Host host) {
        try {
            SnmpUtils snmpUtils = new SnmpUtils("udp:" + host.getName() + "/161");
            // 一、获取目的ip对应的mac地址
            // 1. 获取目标ip段的ipAdEntIfIndex值
            //  Map<String, String> ipAdEntIfIndexMap = snmpUtils.snmpWalk(new OID[]{new OID("1.3.6.1.2.1.4.20.1.2")});

            // 2. 根据ipAdEntIfIndex查询目标ip所属的ifName： 接口列表,包括VLAN id
            Map<String, String> interfaceMap = snmpUtils.snmpWalk(new OID[]{new OID("1.3.6.1.2.1.31.1.1.1.1")});
            /*for (String key: interfaceMap.keySet()) {
                // 获取mac地址字符串，如：a0:36:9f:a3:47:1c
                String macValue = interfaceMap.get(key);
                System.out.println(key + " ----> " + macValue);
            }*/

            // 3. 查询所有分配的ip地址
            // Map<String, String> allIpaddrMap = snmpUtils.snmpWalk(new OID[]{new OID("1.3.6.1.2.1.4.22.1.3")});

            // 4. 根据ipAdEntIfIndex和目的IP地址查询获得目的IP地址的MAC
            /*OID[] oids = new OID[allIpaddrMap.size()];
            int i = 0;
            for (String key: allIpaddrMap.keySet()) {
                oids[i] = new OID("1.3.6.1.2.1.3.1.1.2." + key);
                i++;
            }*/

            // 获取所有端口号，10进制mac地址 ---> 端口号
            Map<String, String> portMap = snmpUtils.snmpWalk(new OID[]{new OID("1.3.6.1.2.1.17.4.3.1.2")});

            // 获取所有mac地址，IP地址 ---> mac地址，如：4285.219.222.189.119 ----> a0:36:9f:a3:47:1c
            Map<String, String> macAddrMap = snmpUtils.snmpWalk(new OID[]{new OID("1.3.6.1.2.1.3.1.1.2")});
            Map<String, Object> resultMap = new HashMap<>();
            for (String key: macAddrMap.keySet()) {
                // 获取mac地址字符串，如：a0:36:9f:a3:47:1c
                String macValue = macAddrMap.get(key);
                // 16进制mac地址转10进制
                String[] hexValues = macValue.split(":");
                String tenMacStr = "";
                for (String hexValue: hexValues) {
                    Integer tenInt = Integer.parseInt(hexValue, 16);
                    tenMacStr += tenInt.toString() + ".";
                }
                tenMacStr = tenMacStr.substring(0, tenMacStr.length()-1);
                // 获取10进制mac地址对应的端口
                String exchagePort = portMap.get(tenMacStr);
                // 根据端口号获取交换机对应的接口，包括vlan
                String interfaceInfo = interfaceMap.get(exchagePort);

                Map<String, String> portAndInterfaceMap = new HashMap<>();
                portAndInterfaceMap.put("port", exchagePort);
                portAndInterfaceMap.put("interface", interfaceInfo);
                key = key.substring(key.indexOf(".")+1);
                resultMap.put(key, portAndInterfaceMap);
            }
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
